package IO.socket;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 *创建一个服务端和一个客户端，进行简单通信
 *
 * @author 15594
 */
public class SocketTest {
    public static void main(String[] args)  {
        server();
    }
    private static void server() {

        ServerSocket serverSocket = null;
        Socket accept = null;
        InputStream inputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            serverSocket = new ServerSocket(8899);

            accept = serverSocket.accept();

            inputStream = accept.getInputStream();



//            这样写会可能出现中文乱码
//            byte [] bytes = new byte[10];
//            int len = 0;
//            while ((len = inputStream.read(bytes))!=-1){
//                String s = new String(bytes, 0, len);
//                System.out.println(s);
//            }

            byteArrayOutputStream = new ByteArrayOutputStream();
            byte [] bytes = new byte[10];
            int len = 0;
            while ((len = inputStream.read(bytes))!=-1){
                byteArrayOutputStream.write(bytes,0,len);
            }
            String string = byteArrayOutputStream.toString();
            System.out.println(string);


        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (byteArrayOutputStream!=null){
                try {
                    byteArrayOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (accept!=null){
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
class Client{
    public static void main(String[] args) {
        client();
    }
    private static void client() {
        Socket socket = null;
        OutputStream outputStream = null;
        try {
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inet, 8899);
            outputStream = socket.getOutputStream();
            outputStream.write("你好,我是客户端".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
